<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>

          <a-col :span="12" >
            <a-form-item label="选择部门" :labelCol="labelCol" :wrapperCol="wrapperCol">

              <j-select-depart :disabled="isshowbm" v-model="bmvalue"/>
              {{ bmvalue }}

            </a-form-item></a-col>

          <a-col :span="12">
            <a-form-item label="评价批次" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                type="list"
                :disabled="dis"
                v-decorator="['pjPc']"
                :trigger-change="true"
                dictCode="pj_pc_yue"
                placeholder="请选择评价批次" />
            </a-form-item>
          </a-col>

          <a-col v-if="istrue" :span="12">
            <a-form-item label="评价员工" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input :disabled="dis" v-decorator="['pjUser']" placeholder="请输入评价员工" ></a-input>
            </a-form-item>
          </a-col>
          <a-col v-if="istrue" :span="12">
            <a-form-item label="成就客户" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-rate disabled v-model="starnum" allow-half @change="changestar"/>
              <a-input-number :disabled="dis" id="inputNumber" v-model="value" :min="1" :max="5" /> 分
            </a-form-item>
          </a-col>
          <a-col v-if="istrue" :span="12">
            <a-form-item label="务实守信" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-rate disabled v-model="starnum2" allow-half @change="changestar2"/>
              <a-input-number :disabled="dis" id="inputNumber" v-model="value2" :min="1" :max="5" /> 分
            </a-form-item>
          </a-col>
          <a-col v-if="istrue" :span="12">
            <a-form-item label="合作共进" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-rate disabled v-model="starnum3" allow-half @change="changestar3"/>
              <a-input-number :disabled="dis" id="inputNumber" v-model="value3" :min="1" :max="5" /> 分
            </a-form-item>
          </a-col>
          <a-col v-if="istrue" :span="12">
            <a-form-item label="创新发展" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-rate disabled v-model="starnum4" allow-half @change="changestar4"/>
              <a-input-number :disabled="dis" id="inputNumber" v-model="value4" :min="1" :max="5" /> 分
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="考核时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date :disabled="dis" placeholder="请选择考核时间" v-decorator="['khTime']" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>

          <a-col v-if="istrue" :span="12">
            <a-form-item label="打分人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input :disabled="dis" v-decorator="['pjDfuser']" placeholder="请输入打分人" ></a-input>
            </a-form-item>
          </a-col>

          <a-col v-if="istrue" :span="24">
            <a-form-item label="列举实例" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea :disabled="dis" v-decorator="['pjLjsl']"></a-textarea>
            </a-form-item>
          </a-col>

          <a-col v-if="istrue" :span="12">
            <a-form-item label="成就客户" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-rate v-model="starnum5" allow-half @change="changestar5"/>
              <a-input-number :disabled="distrue" id="inputNumber" v-model="value5" :min="1" :max="5" /> 分
            </a-form-item>
          </a-col>
          <a-col v-if="istrue" :span="12">
            <a-form-item label="务实守信" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-rate v-model="starnum6" allow-half @change="changestar6"/>
              <a-input-number :disabled="distrue" id="inputNumber" v-model="value6" :min="1" :max="5" /> 分
            </a-form-item>
          </a-col>
          <a-col v-if="istrue" :span="12">
            <a-form-item label="合作共进" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-rate v-model="starnum7" allow-half @change="changestar7"/>
              <a-input-number :disabled="distrue" id="inputNumber" v-model="value7" :min="1" :max="5" /> 分
            </a-form-item>
          </a-col>
          <a-col v-if="istrue" :span="12">
            <a-form-item label="创新发展" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-rate v-model="starnum8" allow-half @change="changestar8"/>
              <a-input-number :disabled="distrue" id="inputNumber" v-model="value8" :min="1" :max="5" /> 分
            </a-form-item>
          </a-col>

          <a-col v-if="istrue" :span="12">
            <a-form-item label="审核人员" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input :disabled="dis" v-decorator="['shUser']" placeholder="请输入审核人员" ></a-input>
            </a-form-item>
          </a-col>
          <a-col v-if="istrue" :span="12">
            <a-form-item label="审核结果" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <!-- <a-input :disabled="dis" v-decorator="['shJg']" placeholder="请输入审核结果" ></a-input> -->
              <a-radio-group v-model="choose" @change="chanradio">
                <a-radio :value="2">
                  通过
                </a-radio>
                <a-radio :value="3">
                  退回
                </a-radio>

              </a-radio-group>

            </a-form-item>
          </a-col>

          <a-col v-if="isshow" :span="24">
            <a-form-item label="退回意见" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea v-decorator="['pjThyj']"></a-textarea>
            </a-form-item>
          </a-col>

          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JDate from '@/components/jeecg/JDate'
  import JDictSelectTag from '@/components/dict/JDictSelectTag'
  import JSelectDepart from '@/components/jeecgbiz/JSelectDepart'

  export default {
    name: 'PjInfohForm',
    components: {
      JSelectDepart,
      JFormContainer,
      JDate,
      JDictSelectTag
    },
    props: {
      // 流程表单data
      formData: {
        type: Object,
        default: () => {},
        required: false
      },
      // 表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      // 表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
          form: this.$form.createForm(this),
        isshowbm: 'true',
        bmvalue: '',
        istrue: true,
        isshow: false,
        choose: 2,
        pjCjkhstarnum: '',
        dis: true,
        distrue: false,
        value: 3,
        value2: 3,
        value3: 3,
        value4: 3,
        value5: 3,
        value6: 3,
        value7: 3,
        value8: 3,
        starnum: 3,
        starnum2: 3,
        starnum3: 3,
        starnum4: 3,
        starnum5: 3,
        starnum6: 3,
        starnum7: 3,
        starnum8: 3,
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: '/pjinfo/pjInfo/add',
          edit: '/pjinfo/pjInfo/edit',
          queryById: '/pjinfo/pjInfo/queryById',
          getallbm: '/pjinfo/pjInfo/getallbm'
        }
      }
    },
    computed: {
      formDisabled() {
        if (this.formBpm === true) {
          if (this.formData.disabled === false) {
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton() {
        if (this.formBpm === true) {
          if (this.formData.disabled === false) {
            return true
          }
        }
        return false
      }
    },
    created () {
      // 如果是流程中表单，则需要加载流程表单data
      this.showFlowData()
     // this.getallbm()
    },
    methods: {

      getFormFieldValue(field) {
              return this.form.getFieldValue(field)
            },

      // 获取所有部门
      getallbm() {
        getAction(this.url.getallbm).then((res) => {
          if (res.success) {

          }
        })
      },

      chanradio(va) {
        console.log(va)
        if (va.target.value == 3) {
          this.isshow = true
        } else {
          this.isshow = false
        }
      },

      changestar(va) {
        console.log(va)
        this.value = va
      },
      changestar2(va) {
        console.log(va)
        this.value2 = va
      },
      changestar3(va) {
        console.log(va)
        this.value3 = va
      },
      changestar4(va) {
        console.log(va)
        this.value4 = va
      },

      changestar5(va) {
        console.log(va)
        this.value5 = va
      },
      changestar6(va) {
        console.log(va)
        this.value6 = va
      },
      changestar7(va) {
        console.log(va)
        this.value7 = va
      },
      changestar8(va) {
        console.log(va)
        this.value8 = va
      },

      add () {
        this.edit({})
      },
      edit (record) {
        console.log(record)

        if (record.shJg != 1) {
              this.choose = parseInt(record.shJg)
        }

        this.value = record.pjCjkh
        this.value2 = record.pjWssx
        this.value3 = record.pjHzgj
        this.value4 = record.pjCxfz

        this.starnum = parseInt(record.pjCjkh)
        this.starnum2 = parseInt(record.pjWssx)
        this.starnum3 = parseInt(record.pjHzgj)
        this.starnum4 = parseInt(record.pjCxfz)

        // 判断对象是否为空
        if (Object.getOwnPropertyNames(record).length == 0) {
          this.isshowbm = false
          this.dis = false
          this.istrue = false
          this.isshow = false
        } else {
          this.isshowbm = true
          this.dis = true
          this.istrue = true
          if (this.choose == 3) {
            this.isshow = true
          } else {
            this.isshow = false
          }
        }
        this.form.resetFields()
        this.model = Object.assign({}, record)
        this.visible = true
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'pjPc', 'pjUser', 'pjCjkh', 'pjWssx', 'pjHzgj', 'pjCxfz', 'pjCjkhSh', 'pjWssxSh', 'pjHzgjSh', 'pjCxfzSh', 'pjLjsl', 'pjDfuser', 'khTime', 'pjThyj', 'shUser', 'shJg'))
        })
      },
      // 渲染流程表单数据
      showFlowData() {
        if (this.formBpm === true) {
          let params = { id: this.formData.dataId }
          getAction(this.url.queryById, params).then((res) => {
            if (res.success) {
              this.edit(res.result)
            }
          })
        }
      },
      submitForm () {
        const that = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true
            let httpurl = ''
            let method = ''
            if (!this.model.id) {
              httpurl += this.url.add
              method = 'post'
            } else {
              httpurl += this.url.edit
               method = 'put'
            }
            this.model.pjBm = this.bmvalue
            this.model.shJg = this.choose
            this.model.pjCjkh = this.value
            this.model.pjWssx = this.value2
            this.model.pjHzgj = this.value3
            this.model.pjCxfz = this.value4
            let formData = Object.assign(this.model, values)
            console.log('表单提交数据', formData)

            // if (formData.pjThyj == null) {
            //   that.$message.warning('请填写必填项！')
            //   that.confirmLoading = false
            //   return
            // }

            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.confirmLoading = false
            })
          }
        })
      },
      popupCallback(row) {
        this.form.setFieldsValue(pick(row, 'pjPc', 'pjUser', 'pjCjkh', 'pjWssx', 'pjHzgj', 'pjCxfz', 'pjCjkhSh', 'pjWssxSh', 'pjHzgjSh', 'pjCxfzSh', 'pjLjsl', 'pjDfuser', 'pjThyj', 'khTime', 'shUser', 'shJg'))
      }
    }
  }
</script>
